package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImagVO;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;
    @PostMapping("/upload")
    public SysResult upload(MultipartFile file) throws IOException{

        ImagVO imagVO = fileService.upload(file);
        if (imagVO==null){//说明业务执行有误
            return SysResult.fail();
        }
        return SysResult.success(imagVO);
    }

    /*@PostMapping("/upload")
    public SysResult upload(MultipartFile file) throws IOException {
        //1、获取文件名
        String filename = file.getOriginalFilename();
        //2、获取文件目录
        String fileDir = "E:/images/";
        //2.1、判断目录是否存在
        File dir = new File(fileDir);
        //如果目录不存在创建目录
        if (!dir.exists()){
            dir.mkdirs();
        }
        //3、准备文件全路径
        String localPath = fileDir+filename;
        //4、实现文件输出
        file.transferTo(new File(localPath));
        System.out.println("文件上传成功");
        return SysResult.success();
    }*/
    @DeleteMapping("/deleteFile")
    public SysResult deleteFile(String virtualPath){
        fileService.deleteFile(virtualPath);
        return SysResult.success();
    }

}
